package com.homework;

import java.util.Vector;

public class EnemyTank extends Tank implements Runnable {
    public EnemyTank(int x, int y, int direction) {
        super(x, y, direction);
    }

    private Vector<EnemyTank> EnemyTanks = null;

    public void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        EnemyTanks = enemyTanks;
    }

    public boolean collision() {
//        for (int i = 0; i < EnemyTanks.size(); i++) {
//            EnemyTank e = EnemyTanks.get(i);
//            if (e != this) {
//                if (Math.abs(e.getDirection() - this.getDirection()) == 1) {//一个上下 一个左右
//                    if (e.getDirection() == 0 || e.getDirection() == 2) {//如果e处于上下方向
//                        if (Math.sqrt(Math.pow(e.getX() + 20 - this.getX() + 30, 2)
//                                + Math.pow(e.getY() + 30 - this.getY() + 20, 2)) < 7)
//                            return true;
//                    } else {//如果e处于左右方向1
//                        if (Math.sqrt(Math.pow(this.getX() + 20 - e.getX() + 30, 2)
//                                + Math.pow(this.getY() + 30 - e.getY() + 20, 2)) < 71)
//                            return true;
//                    }
//                } else {//两者同向
//                    if (Math.sqrt(Math.pow(this.getX() - e.getX(), 2)
//                            + Math.pow(this.getY() - e.getY(), 2)) < 71)
//                        return true;
//                }
//            }
//        }
//        return false;
        return false;
    }

    @Override
    public void run() {
        while (true) {
            if (shots.size() == 0) newShot(this);
            switch (getDirection()) {
                case 0:
                    for (int i = 0; i < (int) (15 + Math.random() * 15); i++) {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if (!collision())
                            moveUp();
                    }
                    break;
                case 1:
                    for (int i = 0; i < (int) (15 + Math.random() * 15); i++) {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if (!collision())
                            moveRight();
                    }
                    break;
                case 2:
                    for (int i = 0; i < (int) (15 + Math.random() * 15); i++) {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if (!collision())
                            moveDown();
                    }
                    break;
                case 3:
                    for (int i = 0; i < (int) (15 + Math.random() * 15); i++) {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if (!collision())
                            moveLeft();
                    }
                    break;
            }
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            setDirection((int) (Math.random() * 4));
            if (!isLive) break;
        }
    }
}
